package net.comm.pic;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.text.SimpleDateFormat;
import java.util.Date;   
 
import magick.CompositeOperator;
import magick.CompressionType;
import magick.DrawInfo;
import magick.ImageInfo;
import magick.MagickApiException;
import magick.MagickException;
import magick.MagickImage;
import magick.PixelPacket;
import magick.PreviewType;   
 
public class Jpicmk {   
 
    static{
        //不能漏掉这个，不然jmagick.jar的路径找不到
        System.setProperty("jmagick.systemclassloader","no");
    }   
 
  
 
    /**
     * 水印(图片logo)
     * @param filePath  源文件路径
     * @param toImg     修改图路径
     * @param logoPath  logo图路径
     * @throws MagickException
     */
    public static void initLogoImg(String filePath, String toImg, String logoPath) throws MagickException {
        ImageInfo info = new ImageInfo();
        MagickImage fImage = null;
        MagickImage sImage = null;
        MagickImage fLogo = null;
        MagickImage sLogo = null;
        Dimension imageDim = null;
        Dimension logoDim = null;
        try {
            fImage = new MagickImage(new ImageInfo(filePath));
            imageDim = fImage.getDimension();
            int width = imageDim.width;
            int height = imageDim.height;
            if (width > 660) {
                height = 660 * height / width;
                width = 660;
            }
            sImage = fImage.scaleImage(width, height);   
 
            fLogo = new MagickImage(new ImageInfo(logoPath));
            logoDim = fLogo.getDimension();
            int lw = width / 8;
            int lh = logoDim.height * lw / logoDim.width;
            sLogo = fLogo.scaleImage(lw, lh);   
 
            sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,  width-(lw + lh/10), height-(lh + lh/10));
            sImage.setFileName(toImg);
            sImage.writeImage(info);
        } finally {
            if(sImage != null){
                sImage.destroyImages();
            }
        }
    }   
 
    /**
     * 水印(文字)
        * @param filePath 源文件路径
     * @param toImg    修改图路径
     * @param text     名字(文字内容自己随意)
     * @throws MagickException
     */
    public static void initTextToImg(String filePath, String toImg,  String text) throws MagickException{
    	
        ImageInfo info = new ImageInfo(filePath);
        if (filePath.toUpperCase().endsWith("JPG") || filePath.toUpperCase().endsWith("JPEG")) {
           info.setCompression(CompressionType.JPEGCompression); //压缩类别为JPEG格式
           info.setPreviewType(PreviewType.JPEGPreview); //预览格式为JPEG格式
           info.setQuality(95);
        }
            MagickImage aImage = new MagickImage(info);
            Dimension imageDim = aImage.getDimension();
            int wideth = imageDim.width;
            int height = imageDim.height;
            if (wideth > 660) {
                height = 660 * height / wideth;
                wideth = 660;
            }
            int a = 0;
            int b = 0;
            String[] as = text.split("");
            for (String string : as) {
                if(string.matches("[\u4E00-\u9FA5]")){
                    a++;
                }
                if(string.matches("[a-zA-Z0-9]")){
                    b++;
                }
            }
            int tl = a*12 + b*6 + 20;
            MagickImage scaled = aImage.scaleImage(wideth, height);
            if(wideth > tl && height > 5){   
//            	System.out.println("tl="+tl);
//            	System.out.println("wideth=" + wideth);
                DrawInfo aInfo = new DrawInfo(info);
                aInfo.setFill(PixelPacket.queryColorDatabase("white"));
                aInfo.setUnderColor(new PixelPacket(0,0,0,100));
                aInfo.setPointsize(12);
                //解决中文乱码问题,自己可以去随意定义个自己喜欢字体，我在这用的微软雅黑
                String fontPath = "C:/WINDOWS/Fonts/MSYH.TTF";
//              String fontPath = "/usr/maindata/MSYH.TTF";
                aInfo.setFont(fontPath);
                aInfo.setTextAntialias(true);
                aInfo.setOpacity(0);
//                aInfo.setText("　" + text + "于　" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "　上传于一点贷网，版权归作者所有！");
                aInfo.setText(text);
                aInfo.setGeometry("+" + (wideth-tl) + "+" + (height-5));
                scaled.annotateImage(aInfo);
            }
            scaled.setFileName(toImg);
            scaled.writeImage(info);
            scaled.destroyImages();
    }   
    /**
     * 压缩图片
     * @param filePath 源文件路径
     * @param toPath   缩略图路径
     * wideth			压缩后宽度 
     * height			压缩后高度
     * sc				是否按比例压缩， 1：按比例，0，不按比例
     */
    public static void createThumbnail(String filePath, String toPath, int wideth, int height, int sc) throws MagickException{
        ImageInfo info = null;
        MagickImage image = null;
        Dimension imageDim = null;
        MagickImage scaled = null;
        try{
            info = new ImageInfo(filePath);
            image = new MagickImage(info);
            imageDim = image.getDimension();
//            int wideth = imageDim.width;
//            int height = imageDim.height;
//            wideth = 110;
//            if (wideth > height) {
            if(sc ==1){
                height = wideth * imageDim.height / imageDim.width;
            }
//            }
            scaled = image.scaleImage(wideth, height);//小图片文件的大小.
            scaled.setFileName(toPath);
            scaled.writeImage(info);
        }finally{
            if(scaled != null){
                scaled.destroyImages();
            }
        }
    } 
    /**
     * 切图
     * @param imgPath 源图路径
     * @param toPath  修改图路径
     * ow	源文件宽度
     * oh   源文件高度
     * @param w
     * @param h
     * @param x
     * @param y
     * sc 	是否按比例 1:按比例 0:不按比例
     * @throws MagickException
     */
    public static void cutImg(String imgPath, String toPath, int ow, int oh, int w, int h, int x, int y,int sc) throws MagickException {
        ImageInfo infoS = null;
        MagickImage image = null;
        MagickImage cropped = null;
        MagickImage scaled = null;
        Dimension imageDim = null;
        Rectangle rect = null;
        try {
            infoS = new ImageInfo(imgPath);
            image = new MagickImage(infoS);
            imageDim = image.getDimension();
            if(sc == 1){
                scaled = image.scaleImage(ow, ow * imageDim.height / imageDim.width);		//源文件压缩
            }else{
                scaled = image.scaleImage(ow, oh);		//源文件压缩
            }
//            if(sc == 1){
//                rect = new Rectangle(x, y, w, w * imageDim.height / imageDim.width);
//            }else{
                rect = new Rectangle(x, y, w, h);
//            }
            
            cropped = scaled.cropImage(rect);		//在压缩的基础上做裁剪
            
            cropped.setFileName(toPath);
            cropped.writeImage(infoS);   
 
        }catch(MagickApiException ex){
    		ex.printStackTrace();
    	} catch(MagickException   ex)   {
    		ex.printStackTrace();
    	} finally {
            if (cropped != null) {
                cropped.destroyImages();
            }
            if (scaled != null) {
            	scaled.destroyImages();
            }
        }
    }
 
    
    public static void main(String[] args) {
    	String picFrom = "d:/s01250229155550self.gif";
    	String picTo = "d:/12.gif";
    	
    	try{
        	Jpicmk.createThumbnail(picFrom, picTo, 110,135,1);
//        	Jpicmk.cutImg(picFrom, picTo, 110,135, 100, 110,10, 10,1);
    		
//        	ImageInfo info=new ImageInfo(picFrom);
//    		MagickImage image=new MagickImage(new ImageInfo(picFrom));
//    		MagickImage scaled=image.scaleImage(120, 97);
//    		scaled.setFileName(picTo);
//    		scaled.writeImage(info);
    	}catch(MagickApiException ex){
    		ex.printStackTrace();
    	} catch(MagickException   ex) {
    		ex.printStackTrace();
    	}
    }
    
}
